package com.xiaoq.distribution.repository.impl;

import com.xiaoq.distribution.entity.TicketSettleEntity;
import com.xiaoq.distribution.repository.ITicketSettleRepository;
import com.xiaoq.vo.PagingVo;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * Created by chenjh on 2018/5/24.
 */
@Repository
public class TicketSettleRepositoryImpl implements ITicketSettleRepository {

    @Autowired
    private Datastore datastore;

    @Override
    public boolean save(TicketSettleEntity ticketSettleEntity) {
        if (ticketSettleEntity == null || StringUtils.isBlank(ticketSettleEntity.getId())){
            return false;
        }
        datastore.save(ticketSettleEntity);
        return true;
    }

    @Override
    public List<TicketSettleEntity> getTicketSettleList(String settleHistoryId, PagingVo pagingVo) {
        Query<TicketSettleEntity> settleQuery = datastore.createQuery(TicketSettleEntity.class);

        if(StringUtils.isNotBlank(settleHistoryId)){
            settleQuery.filter("settleHistoryId", settleHistoryId);
        }

        if(pagingVo != null){
            settleQuery.offset(pagingVo.getPageOffset()).limit(pagingVo.getPs());
        }

        return settleQuery.asList();
    }
}
